﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace DataTier
{

    [DataContract(IsReference = true)]
    public class Route
    {

        private IList<Connection> _connections;
        private int _cost;
        private string _identifier;

        public Route(string identifier)
        {
            _connections = new List<Connection>();
            _cost = int.MaxValue;
            _identifier = identifier;
        }

        [DataMember]
        public IList<Connection> Connections
        {
            get { return _connections; }
            set { _connections = value; }
        }

        [DataMember]
        public int Cost
        {
            get { return _cost; }
            set { _cost = value; }
        }

        [DataMember]
        public IList<string> Description
        {
            get
            {
                return GetDescription();
            }
        }

        public IList<string> GetDescription()
        {

            List<string> steps = new List<string>();
            int count = 0;

            foreach (Connection connection in _connections)
            {

                count++;
                string step = count.ToString() + ". Drive to " + connection.To.ToString() + " (" + connection.Weight.ToString() + " km)";
                steps.Add(step);

            }

            steps.Add(string.Empty);
            steps.Add("Arrived at destination. Total distance: " + _cost.ToString() + " km");

            return steps;

        }

        public override string ToString()
        {

            if (_connections.Count > 0)
            {

                string route = "";
                int count = 0;

                foreach (Connection c in _connections)
                {

                    route += c.To.ToString();

                    if (count < _connections.Count - 1)
                    {
                        route += " - ";
                    }

                    count++;

                }

                return route;

            }
            else
            {
                return "Empty";
            }

        }

    }
}
